//------------------------------------------------------------------------------
#include <QMessageBox>
#include <QDate>
#include <QDesktopServices>
#include <QUuid>
#include <QFileDialog>
#include <QFileInfo>
#include <QFile>
#include <QByteArray>
#include <QUrl>
#include <QtGui>
#include <QSqlRecord>
#include <QMovie>
#include "CClientFrm.h"
#include "CMyTableWidgetItem.h"
#include "CDiff.h"
#include "CFindFrm.h"
#include "CComboResultFrm.h"
#include "CGestionFavorisClientFrm.h"
#include "CComboDelegate.h"
#include "CEcheancierFrm.h"
#include "CPrintFrm.h"
#include "version.h"
#include "CTextEditDelegate.h"
#include "CTimeEditDelegate.h"
#include "CCalendrierFrm.h"
#include "CMySQL.h"
#include "CHelp.h"
#include "CMimeType.h"
//------------------------------------------------------------------------------
CClientFrm::CClientFrm(QString name, QString lastCode, CMainFrm *parent) : CMyMainWindow(name, parent) {
	setActionCur(CGlobal::acConstruct);
	setupUi(this);
	setStatusBar(NULL);
	
	masterTable="client";
	primaryKey="code";
	mainFrm=parent;
	
	effectif->addItem(tr("1 à 5"));
	effectif->addItem(tr("6 à 9"));
	effectif->addItem(tr("10 à 19"));
	effectif->addItem(tr("20 à 49"));
	effectif->addItem(tr("50 à 99"));
	effectif->addItem(tr("100 à 199"));
	effectif->addItem(tr("plus de 200"));
	
	documentPar->addItem(tr("MAIL"));
	documentPar->addItem(tr("FAX"));
	documentPar->addItem(tr("COURRIER"));
	
	tabWidget->removeTab(0);
	
	createGrilleActions();
	createGrilleContact();
	createGrilleAdresses();
	createGrilleDevis();
	createGrilleCommandes();
	createGrilleBLs();
	createGrilleFactures();
	createGrillePrixArticle();
	createGrilleDocuments();
	createGrilleEcheances();
	createGrilleMateriels();
	createGrilleTva();
	
	cmtrsWidget=new CTextRevWidget(this);
	cmtrsWidget->setChamp("commentaires");
	cmtrsWidget->setTable("client");
	connect(cmtrsWidget, SIGNAL(textChanged()), this, SLOT(onCmtrsChange()));
	connect(cmtrsWidget, SIGNAL(invokeHelp(CChamp*)), this, SLOT(onInvokeHelp(CChamp*)));
	connect(cmtrsWidget, SIGNAL(loadRevision(QString)), this, SLOT(onLoadCmtrsRevision(QString)));
	tabWidget->addTab(cmtrsWidget, QIcon(), tr("Commentaires"));
	
	makeMenu();
	
	loadSchema();
	
	loadPrintPopupMenu(toolBar);
	
	if(CMySQL::getInstance()->getNbEnregistrement(masterTable) == 0) {
		setActionCur(CGlobal::acTableVide);
	}else {
		if(!chargeDonnees(lastCode, CGlobal::seNone)) chargeDonnees("", CGlobal::sePremier);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::setActionCur(CGlobal::EAction action) {
	actionCur=action;
	
	switch(actionCur) {
		case CGlobal::acNone:
			actPremiereFiche->setEnabled(true);
			actFichePrecedente->setEnabled(true);
			actFicheSuivante->setEnabled(true);
			actDerniereFiche->setEnabled(true);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(true);
			actProspect->setEnabled(true);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(true);
			actEvolution->setEnabled(true);
			
			actAbonneEMailing->setEnabled(true);

			actFavorisClient->setEnabled(true);
			actEcheancier->setEnabled(true);
			
			enableChamps(true);
			code->setEnabled(false);
			tabWidget->setEnabled(true);
			effectif->setEnabled(true);
			documentPar->setEnabled(true);
			
			enableBtnPrint(true);
			
			responsableModif=false;
			cmtrsChange=false;
			break;
		case CGlobal::acTableVide:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(false);
			actProspect->setEnabled(true);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(false);
			actEvolution->setEnabled(false);
			
			actAbonneEMailing->setEnabled(false);

			actFavorisClient->setEnabled(false);
			actEcheancier->setEnabled(false);
			
			clearChamps();
			enableChamps(false);
			creation->setValue("");
			prospect->setValue("");
			tabWidget->setEnabled(false);
			effectif->setEnabled(false);
			documentPar->setEnabled(false);
			
			enableBtnPrint(false);
			
			responsableModif=false;
			cmtrsChange=false;
			break;
		case CGlobal::acAjout:
		case CGlobal::acAjoutFromOther:
			clearChamps();
			contact->setText("");
			fonction->setText("");
			mail->setText("");
			initChamps();
			enableChamps(true);
			code->setFocus();
			cmtrsWidget->clearRevision();
			
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(false);
			actSupprimer->setEnabled(false);
			actProspect->setEnabled(false);
			
			actValider->setEnabled(true);
			actAnnuler->setEnabled(true);
			
			actRechercheUneFiche->setEnabled(false);
			actEvolution->setEnabled(false);
			
			actAbonneEMailing->setEnabled(false);

			actFavorisClient->setEnabled(false);
			actEcheancier->setEnabled(false);
			
			tabWidget->setEnabled(false);
			effectif->setEnabled(true);
			documentPar->setEnabled(true);
			
			enableBtnPrint(false);
			
			responsableModif=false;
			cmtrsChange=false;
			break;
		case CGlobal::acModif:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(false);
			actSupprimer->setEnabled(false);
			actProspect->setEnabled(false);
			
			actValider->setEnabled(true);
			actAnnuler->setEnabled(true);
			
			actRechercheUneFiche->setEnabled(false);
			actEvolution->setEnabled(false);
			
			actAbonneEMailing->setEnabled(false);

			actFavorisClient->setEnabled(false);
			actEcheancier->setEnabled(false);

			tabWidget->setEnabled(true);
			effectif->setEnabled(true);
			documentPar->setEnabled(true);
			
			enableBtnPrint(false);
			
			responsableModif=false;
			cmtrsChange=false;
		default:
			break;
	}
}
//------------------------------------------------------------------------------
CGlobal::EAction CClientFrm::getActionCur(void) {
	return actionCur;
}
//------------------------------------------------------------------------------
void CClientFrm::closeEvent(QCloseEvent * event) {
	if(getActionCur() != CGlobal::acNone && getActionCur() != CGlobal::acTableVide) {
		QMessageBox msgBox(	QMessageBox::Warning, tr("Attention"), 
							tr("Veuillez annuler ou valider avant de sortir"), QMessageBox::Ok);
		msgBox.exec();	
		
		event->ignore();
	}else {
		emit quit(objectName(), curCode);
		event->accept();
	}
}
//------------------------------------------------------------------------------
void CClientFrm::loadProspectInfo(QString codeProspect) {
	QString requete;
	QSqlQuery *query;
	
	requete="SELECT code, nom, adresse1, adresse2, adresse3, codePostal, ville, pays, telephone, fax, activite, effectif, commentaires from prospect ";
	requete+=" WHERE code = \""+codeProspect+"\"";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		if(query->next()) {
			QSqlRecord rec=query->record();
			
			code->setValue(rec.value("code"));
			nom->setValue(rec.value("nom"));
			adresse1->setValue(rec.value("adresse1"));
			adresse2->setValue(rec.value("adresse2"));
			adresse3->setValue(rec.value("adresse3"));
			codePostal->setValue(rec.value("codePostal"));
			ville->setValue(rec.value("ville"));
			pays->setValue(rec.value("pays"));
			telephone->setValue(rec.value("telephone"));
			fax->setValue(rec.value("fax"));
			prospect->setValue(codeProspect);
			activite->setValue(rec.value("activite"));
			effectif->setCurrentIndex(effectif->findText(rec.value("effectif").toString()));
			documentPar->setCurrentIndex(0);
			cmtrsWidget->setValue(rec.value("commentaires").toString());
			if(rec.value("commentaires").toString() != "")
			{
				cmtrsChange=true;
			}
		}
	}
	delete query;
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleActions(void) {
	CTextEditDelegate *delegateCmtr=new CTextEditDelegate();
	CTimeEditDelegate *delegateTime=new CTimeEditDelegate();
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelInt(cnacNum, "numeroAction", tr("Numéro"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnacAction, "action", tr("Action")));
	columnModel->append(new CColumnModelDate(cnacDate, "dateAction", tr("Date"), Qt::AlignRight, 
			new QIcon(":coMexpert/images/1day.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelTime(cnacHeure, "heureAction", tr("Heure"), Qt::AlignRight));
	columnModel->append(new CColumnModelString(cnacQui, "qui", tr("Par"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnacInterloc, "interlocuteur", tr("Interlocuteur"), Qt::AlignLeft,
			new QIcon(":coMexpert/images/aim_protocol.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnacCommentaires, "commentaires", tr("Commentaires")));
	
	actionWidget=new CGrilleWidget(columnModel, this);
	connect(actionWidget, SIGNAL(ajoute(bool&)), this, SLOT(ajouteAction(bool&)));
	connect(actionWidget, SIGNAL(supprime(int, bool&)), this, SLOT(supprimeAction(int, bool&)));
	connect(actionWidget, SIGNAL(cellChanged(int, int)), this, SLOT(modifiAction(int, int)));
	connect(actionWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(actionDoubleClicked(int, int)));
	connect(actionWidget, SIGNAL(beforeInsert(const QSqlQuery *, CMyTableWidgetItem *)), this, 
			SLOT(beforeInsertAction(const QSqlQuery *, CMyTableWidgetItem *)));
	
	tabWidget->addTab(actionWidget, QIcon(":coMexpert/images/aktion.png"), tr("Actions"));
	
	actionWidget->setColumnWidth(cnacNum, 80);
	actionWidget->setColumnWidth(cnacAction, 175);
	actionWidget->setColumnWidth(cnacInterloc, 175);
	actionWidget->setColumnWidth(cnacCommentaires, 500);
	
	actionWidget->setItemDelegateForColumn(cnacCommentaires, delegateCmtr);
	actionWidget->setItemDelegateForColumn(cnacHeure, delegateTime);
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleActions(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT numeroAction, action, dateAction, heureAction, qui, interlocuteur, commentaires, auto ";
	requete+="FROM actionsClient WHERE client=\""+curCode+"\" ORDER BY dateAction DESC, heureAction DESC";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		actionWidget->loadGrille(query);
	}
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleContact(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelInt(cnccNum, "numero", tr("Numéro"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnccDefaut, "defaut", tr("Def."), Qt::AlignCenter, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnccNom, "nom", tr("Nom")));
	columnModel->append(new CColumnModelString(cnccFonction, "fonction", tr("Fonction")));
	columnModel->append(new CColumnModelString(cnccTel, "telephone", tr("Téléphone(s)")));
	columnModel->append(new CColumnModelString(cnccFax, "fax", tr("Fax")));
	columnModel->append(new CColumnModelString(cnccMail, "mail", tr("Mail")));
	
	contactWidget=new CGrilleWidget(columnModel, this);
	connect(contactWidget, SIGNAL(ajoute(bool&)), this, SLOT(ajouteContact(bool&)));
	connect(contactWidget, SIGNAL(supprime(int, bool&)), this, SLOT(supprimeContact(int, bool&)));
	connect(contactWidget, SIGNAL(cellChanged(int, int)), this, SLOT(modifiContact(int, int)));
	connect(contactWidget, SIGNAL(customContextMenuRequested(const QPoint &)),
			this, SLOT(tableCustomContextMenuRequested(const QPoint &)));
	connect(contactWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(contactDoubleClicked(int, int)));
			tabWidget->addTab(contactWidget, QIcon(":coMexpert/images/aim_protocol.png"), tr("Contacts"));
	connect(contactWidget, SIGNAL(beforeChangeRecord(const QSqlQuery *, int)), this, SLOT(beforeChangeRecordContact(const QSqlQuery *, int)));
	
	tabWidget->addTab(contactWidget, QIcon(":coMexpert/images/aim_protocol.png"), tr("Contacts"));
	
	contactWidget->setColumnWidth(cnccNum, 80);
	contactWidget->setColumnWidth(cnccDefaut, 30);
	contactWidget->setColumnWidth(cnccNom, 200);
	contactWidget->setColumnWidth(cnccFonction, 200);
	contactWidget->setColumnWidth(cnccMail, 250);
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleContact(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT numero, defaut, nom, fonction, telephone, fax, mail ";
	requete+="FROM contactClient WHERE client=\""+curCode+"\" ORDER BY numero";
	
	contact->setText("");
	fonction->setText("");
	mail->setText("");
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		int nb=contactWidget->loadGrille(query);
		tabWidget->setTabText(etnContacts, tr("Contacts")+" ("+QString::number(nb)+")");
	}
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleAdresses(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelInt(cnadcNum, "numero", tr("Numéro"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnadcNom, "nom", tr("Nom à l'adresse")));
	columnModel->append(new CColumnModelString(cnadcAdresse1, "adresse1", tr("Adresse")));
	columnModel->append(new CColumnModelString(cnadcAdresse2, "adresse2", tr("Adresse")));
	columnModel->append(new CColumnModelString(cnadcAdresse3, "adresse3", tr("Adresse")));
	columnModel->append(new CColumnModelString(cnadcCodePostal, "codePostal", tr("Code postal")));
	columnModel->append(new CColumnModelString(cnadcVille, "ville", tr("Ville")));
	columnModel->append(new CColumnModelString(cnadcPays, "pays", tr("Pays"), Qt::AlignLeft, 
												  new QIcon(":coMexpert/images/agt_internet.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelInt(	cnadcDistance, "distance", tr("Distance"), Qt::AlignRight, 
												NULL, Qt::ItemIsEnabled));
	
	adressesWidget=new CGrilleWidget(columnModel, this);
	connect(adressesWidget, SIGNAL(ajoute(bool&)), this, SLOT(ajouteAdresse(bool&)));
	connect(adressesWidget, SIGNAL(supprime(int, bool&)), this, SLOT(supprimeAdresse(int, bool&)));
	connect(adressesWidget, SIGNAL(cellChanged(int, int)), this, SLOT(modifiAdresse(int, int)));
	connect(adressesWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(adresseDoubleClicked(int, int)));
	
	tabWidget->addTab(adressesWidget, QIcon(":coMexpert/images/gohome.png"), tr("Adresses"));
	
	adressesWidget->setColumnWidth(cnadcNum, 80);
	adressesWidget->setColumnWidth(cnadcNom, 200);
	adressesWidget->setColumnWidth(cnadcAdresse1, 300);
	adressesWidget->setColumnWidth(cnadcAdresse2, 60);
	adressesWidget->setColumnWidth(cnadcAdresse3, 60);
	adressesWidget->setColumnWidth(cnadcCodePostal, 100);
	adressesWidget->setColumnWidth(cnadcVille, 200);
	adressesWidget->setColumnWidth(cnadcPays, 120);
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleAdresses(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT numero, nom, adresse1, adresse2, adresse3, codePostal, ville, pays ";
	requete+="FROM adressesClient WHERE client=\""+curCode+"\" ORDER BY numero";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		adressesWidget->loadGrille(query);
	}
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleDevis(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelInt(cndcNum, "code", tr("Numéro"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndcTitre, "titre", tr("Tritre"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDate(cndcDate, "creation", tr("Date"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndcPar, "user", tr("Fait par"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDouble(cndcMontant, "montant", tr("Montant"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndcStatus, "status", tr("Status"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndcCommande, "commandes", tr("Commandes"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndcBL, "bls", tr("BLs"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndcFactures, "factures", tr("Factures"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	
	devisWidget=new CGrilleWidget(columnModel, this, false);
	connect(devisWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(devisDoubleClicked(int, int)));
	
	tabWidget->addTab(devisWidget, QIcon(":coMexpert/images/colorize.png"), tr("Devis"));
	
	devisWidget->setColumnWidth(cndcTitre, 300);
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleDevis(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT code, titre, creation, user, IFNULL(sum(quantite*prixUnitaireHt), 0.00) as montant, status, ";
	requete+="getCommandesFromDevis(code) AS commandes, getBlsFromDevis(code) AS bls, getFacturesFromDevis(code) AS factures ";
	requete+="FROM teteDevis LEFT JOIN ligneDevis ON devis=code ";
	requete+="WHERE codeClient=\""+curCode+"\" GROUP BY code ORDER BY creation DESC, code";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		devisWidget->loadGrille(query);
	}
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleCommandes(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelString(cncocNom, "nomCommande", tr("Nom"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDate(cncocDate, "creation", tr("Date"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cncocDevis, "nomDevis", tr("Devis de ref."), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDouble(cncocMontant, "montant", tr("Montant"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cncocBL, "bls", tr("BLs"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cncocFactures, "factures", tr("factures"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	
	commandesWidget=new CGrilleWidget(columnModel, this, false);
	connect(commandesWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(commandesDoubleClicked(int, int)));
	tabWidget->addTab(commandesWidget, QIcon(":coMexpert/images/package_games_kids.png"), tr("Commandes"));
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleCommandes(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT tc.nomCommande, tc.creation, td.nomDevis, IFNULL(sum(quantite*prixUnitaireHt), 0.00) as montant, ";
	requete+="getBlsFromCommande(tc.code) AS bls, getFacturesFromCommande(tc.code) AS factures, tc.code ";
	requete+="FROM teteCommandes AS tc ";
	requete+="LEFT JOIN ligneCommandes AS lc ON lc.commande=tc.code ";
	requete+="LEFT JOIN teteDevis AS td ON td.code=tc.devis ";
	requete+="WHERE tc.codeClient=\""+curCode+"\" ";
	requete+="GROUP BY tc.code ";
	requete+="ORDER BY tc.creation DESC, tc.code";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		commandesWidget->loadGrille(query, "code");
	}
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleBLs(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelString(cnbcNom, "nomBL", tr("Nom"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDate(cnbcDate, "creation", tr("Date"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnbcDevis, "nomDevis", tr("Devis de ref."), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnbcCommande, "nomCommande", tr("Commande de ref."), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnbcFacture, "factures", tr("Factures"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDouble(cnbcMontant, "montant", tr("Montant"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	
	blsWidget=new CGrilleWidget(columnModel, this, false);
	connect(blsWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(blsDoubleClicked(int, int)));
	
	tabWidget->addTab(blsWidget, QIcon(":coMexpert/images/outbox1.png"), tr("Bons de livraison"));
	
	blsWidget->setColumnWidth(cnbcCommande, 120);
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleBLs(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT tb.nomBL, tb.creation, td.nomDevis, tc.nomCommande, getFacturesFromBL(tb.code) AS factures, ";
	requete+="IFNULL(sum(lb.quantite*lb.prixUnitaireHt), 0.00) as montant, tb.code ";
	requete+="FROM teteBLs AS tb ";
	requete+="LEFT JOIN ligneBLs AS lb ON lb.bl=tb.code ";
	requete+="LEFT JOIN teteDevis AS td ON td.code=tb.devis ";
	requete+="LEFT JOIN teteCommandes AS tc ON tc.code=tb.commande ";
	requete+="WHERE tb.codeClient=\""+curCode+"\" GROUP BY tb.code ORDER BY tb.creation DESC, tb.code";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		blsWidget->loadGrille(query, "code");
	}
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleFactures(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelInt(cnfcNum, "code", tr("Numéro"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDate(cnfcDate, "creation", tr("Date"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDate(cnfcEcheance, "echeance", tr("Echéance"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnfcDevis, "devis", tr("Devis de ref."), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnfcCommande, "commande", tr("Commandes de ref."), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnfcBL, "bls", tr("Bl(s) de ref."), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDouble(cnfcMontant, "montant", tr("Montant"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	
	facturesWidget=new CGrilleWidget(columnModel, this, false);
	connect(facturesWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(facturesDoubleClicked(int, int)));
	
	tabWidget->addTab(facturesWidget, QIcon(":coMexpert/images/money.png"), tr("Factures"));
	
	facturesWidget->setColumnWidth(cnfcCommande, 130);
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleFactures(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT code, creation, echeance, getDevisFromFacture(code) AS devis, ";
	requete+="getCommandesFromFacture(code) AS commandes, getBlsFromFacture(code) AS BLs, ";
	requete+="IFNULL(sum(quantite*prixUnitaireHt*IF(document = \"F\", 1, -1)), 0.00) as montant ";
	requete+="FROM teteFactures INNER JOIN ligneFactures ON facture=code ";
	requete+="WHERE codeClient=\""+curCode+"\" GROUP BY code ORDER BY creation DESC, code";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		facturesWidget->loadGrille(query);
	}
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrillePrixArticle(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelInt(cnccNum, "numero", tr("Numéro"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnpaArticle, "article", tr("Article"), Qt::AlignLeft, 
			new QIcon(":coMexpert/images/camera.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDouble(cnpaPrix, "prix", tr("Prix")));
	
	prixArticleWidget=new CGrilleWidget(columnModel, this);
	connect(prixArticleWidget, SIGNAL(ajoute(bool&)), this, SLOT(ajoutePrixArticle(bool&)));
	connect(prixArticleWidget, SIGNAL(supprime(int, bool&)), this, SLOT(supprimePrixArticle(int, bool&)));
	connect(prixArticleWidget, SIGNAL(cellChanged(int, int)), this, SLOT(modifiPrixArticle(int, int)));
	connect(prixArticleWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(lignePrixArticleDoubleClicked(int, int)));
	
	tabWidget->addTab(prixArticleWidget, QIcon(":coMexpert/images/camera_unmount---.png"), tr("Prix par article"));
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrillePrixArticle(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT numero, article, prix ";
	requete+="FROM prixArticleClient WHERE client=\""+curCode+"\" ORDER BY numero";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		prixArticleWidget->loadGrille(query);
	}
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleDocuments(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelInt(cnpdNum, "numero", tr("Numéro"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnpdDescription, "description", tr("Description")));
	columnModel->append(new CColumnModelString(cnpdDocument, "fileName", tr("Document"), Qt::AlignLeft, 
			new QIcon(":coMexpert/images/doc.png"), Qt::ItemIsEnabled));
	
	documentsWidget=new CGrilleWidget(columnModel, this);
	connect(documentsWidget, SIGNAL(ajoute(bool&)), this, SLOT(ajouteDocument(bool&)));
	connect(documentsWidget, SIGNAL(supprime(int, bool&)), this, SLOT(supprimeDocument(int, bool&)));
	connect(documentsWidget, SIGNAL(cellChanged(int, int)), this, SLOT(modifiDocument(int, int)));
	connect(documentsWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(documentDoubleClicked(int, int)));
	tabWidget->addTab(documentsWidget, QIcon(":coMexpert/images/doc.png"), tr("Documents"));
	
	documentsWidget->setColumnWidth(cnpdNum, 80);
	documentsWidget->setColumnWidth(cnpdDescription, 400);
	documentsWidget->setColumnWidth(cnpdDocument, 200);
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleDocuments(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT numero, description, fileName ";
	requete+="FROM documentsClient WHERE client=\""+curCode+"\" ORDER BY numero";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		documentsWidget->loadGrille(query);
	}
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleEcheances(void) {
	QStringList ouiNon; 
	ouiNon << "OUI" << "NON";
	CComboDelegate *delegateMail=new CComboDelegate(ouiNon);
	CTextEditDelegate *delegateCmtr=new CTextEditDelegate();
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelInt(cnecNum, "numero", tr("Numéro"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnecAction, "codeAction", tr("Action"), Qt::AlignLeft, 
			new  QIcon(":coMexpert/images/aktion.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnecDescription, "description", tr("Description")));
	columnModel->append(new CColumnModelDate(cnecDate, "dateEcheance", tr("Echéance"), Qt::AlignRight, 
			new QIcon(":coMexpert/images/1day.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelInt(cnecRepete, "repete", tr("Périodicité (mois)")));
	columnModel->append(new CColumnModelString(cnecDestinataire, "destinataire", tr("Responsable"), Qt::AlignLeft, 
			new 	QIcon(":coMexpert/images/identity.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnecMail, "mailAEcheance", tr("Mail")));
	columnModel->append(new CColumnModelString(cnecCmtr, "commentaires", tr("Commentaires")));

	columnModel->at(cnecMail)->setDefaultValue("NON");
	
	echeancesWidget=new CGrilleWidget(columnModel, this);
	connect(echeancesWidget, SIGNAL(ajoute(bool&)), this, SLOT(ajouteEcheance(bool&)));
	connect(echeancesWidget, SIGNAL(supprime(int, bool&)), this, SLOT(supprimeEcheance(int, bool&)));
	connect(echeancesWidget, SIGNAL(cellChanged(int, int)), this, SLOT(modifiEcheance(int, int)));
	connect(echeancesWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(echeanceDoubleClicked(int, int)));
	tabWidget->addTab(echeancesWidget, QIcon(":coMexpert/images/alarmd.png"), tr("Echéances"));
	
	echeancesWidget->setColumnWidth(cnecNum, 80);
	echeancesWidget->setColumnWidth(cnecDescription, 300);
	echeancesWidget->setColumnWidth(cnecDestinataire, 100);
	echeancesWidget->setColumnWidth(cnecRepete, 150);
	echeancesWidget->setColumnWidth(cnecMail, 50);
	echeancesWidget->setColumnWidth(cnecCmtr, 400);

	echeancesWidget->setItemDelegateForColumn(cnecMail, delegateMail);
	echeancesWidget->setItemDelegateForColumn(cnecCmtr, delegateCmtr);
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleEcheances(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT numero, codeAction, description, dateEcheance, repete, destinataire, mailAEcheance, commentaires ";
	requete+="FROM echeancesClient WHERE client=\""+curCode+"\" ORDER BY numero";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		echeancesWidget->loadGrille(query);
	}
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleMateriels(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelInt(cnmcNum, "numero", tr("Numéro"),  Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnmcRefMat, "refMat", tr("Ref. fournisseur")));
	columnModel->append(new CColumnModelString(cnmcDesc, "description", tr("Description")));
	columnModel->append(new CColumnModelString(cnmcNumSerie, "numSerie", tr("N° de série")));
	columnModel->append(new CColumnModelString(cnmcFournisseur, "fournisseur", tr("Fournisseur")));
	columnModel->append(new CColumnModelString(cnmcFactureFournisseur, "factureFournisseur", tr("Facture fournisseur")));
	columnModel->append(new CColumnModelInt(cnmcFacture, "facture", tr("Facture"), Qt::AlignRight,
			new QIcon(":coMexpert/images/money.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDate(cnmcDateFinGarantie, "dateFinGarantie", tr("Date de fin de garantie"), Qt::AlignRight,
			new QIcon(":coMexpert/images/1day.png"), Qt::ItemIsEnabled));
	
	materielsWidget=new CGrilleWidget(columnModel, this);
	connect(materielsWidget, SIGNAL(ajoute(bool&)), this, SLOT(ajouteMateriel(bool&)));
	connect(materielsWidget, SIGNAL(supprime(int, bool&)), this, SLOT(supprimeMateriel(int, bool&)));
	connect(materielsWidget, SIGNAL(cellChanged(int, int)), this, SLOT(modifiMateriel(int, int)));
	connect(materielsWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(materielDoubleClicked(int, int)));
	
	tabWidget->addTab(materielsWidget, QIcon(":coMexpert/images/harddrive2.png"), tr("Matériels"));
	
	materielsWidget->setColumnWidth(cnmcNum, 80);
	materielsWidget->setColumnWidth(cnmcRefMat, 120);
	materielsWidget->setColumnWidth(cnmcDesc, 500);
	materielsWidget->setColumnWidth(cnmcNumSerie, 120);
	materielsWidget->setColumnWidth(cnmcFournisseur, 120);
	materielsWidget->setColumnWidth(cnmcFactureFournisseur, 120);
	materielsWidget->setColumnWidth(cnmcFacture, 80);
	materielsWidget->setColumnWidth(cnmcDateFinGarantie, 120);
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleMateriels(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT numero, refMat, description, numSerie, fournisseur, factureFournisseur, facture, dateFinGarantie ";
	requete+="FROM materielsClient WHERE client=\""+curCode+"\" ORDER BY numero";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		materielsWidget->loadGrille(query);
	}
	delete query;
	
	materielsWidget->resizeRowsToContents();
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::createGrilleTva(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelInt(ctncId, "id", tr("Identifiant"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(ctncNom, "nom", tr("Nom"), Qt::AlignLeft));
	columnModel->append(new CColumnModelDouble(ctncTaux, "taux", tr("Taux"), Qt::AlignRight, NULL, (Qt::ItemFlags)63, QVariant(3)));
	columnModel->append(new CColumnModelString(ctncCompteComptable, "compteComptable", tr("Compte Comptable")));
	
	tvaWidget=new CGrilleWidget(columnModel, this, false);
	connect(tvaWidget, SIGNAL(cellChanged(int, int)), this, SLOT(modifiTva(int, int)));
	tabWidget->addTab(tvaWidget, tr("Tva"));
	
	tvaWidget->setColumnWidth(ctncNom, 250);
	tvaWidget->setColumnWidth(ctncCompteComptable, 180);
}
//------------------------------------------------------------------------------
void CClientFrm::loadGrilleTva(void) {
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT id, nom, taux, compteComptable FROM tvaClient WHERE codeClient=\""+curCode+"\" ORDER BY idTva";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		tvaWidget->loadGrille(query);
	}
	
	delete query;
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CClientFrm::creerDocument(int row) {
	setCursor(Qt::WaitCursor);
	QString fileName=QFileDialog::getOpenFileName(this, tr("Choisir un fichier"));
			
	if(fileName != "") {
		QFileInfo fi(fileName);
		QString mimeType=CMimeType::getInstance()->getMimeType(fi.suffix());
		QFile file(fileName);
					
		fileName=fi.fileName();
					
		if(file.open(QIODevice::ReadOnly)) {
			QByteArray ba=file.readAll();
			QString requete;
			QString data="0x"+QString(ba.toHex());
						
			file.close();
						
			requete="UPDATE documentsClient ";
			requete+="SET document="+data+", typeMime=\""+mimeType+"\", fileName=\""+fileName+"\", ";
			requete+="modificateur=\""+CMySQL::getInstance()->getUser()+"\", modification=CURDATE() ";
			requete+="WHERE client=\""+curCode+"\" AND numero="+documentsWidget->item(row, cnpdNum)->text();
						
			documentsWidget->item(row, cnpdDocument)->setText(fileName);
			
			CMySQL::getInstance()->execQuery(requete);
		}
	}
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CClientFrm::makeMenu(void) {
	popupMenu=new QMenu(contactWidget);
	
	actActionFromContact=new QAction(QIcon(":coMexpert/images/bookmark_add.png"), tr("&Créer une action pour ce contact"), this);
	actActionFromContact->setIconVisibleInMenu(true);
	connect(actActionFromContact, SIGNAL(triggered()), this, SLOT(actActionFromContact_triggered()));
	
	actMailFromContact=new QAction(QIcon(":coMexpert/images/mail_post_to.png"), tr("&Envoyer un mail au contact"), this);
	actMailFromContact->setIconVisibleInMenu(true);
	connect(actMailFromContact, SIGNAL(triggered()), this, SLOT(actMailFromContact_triggered()));
	
	popupMenu->addAction(actActionFromContact);
	popupMenu->addAction(actMailFromContact);

	favorisMenu=new QMenu(this);
	
	actNouveauFavoris=new QAction(QIcon(":coMexpert/images/bookmark_add.png"), tr("&Ajouter ce client comme favoris"), this);
	actNouveauFavoris->setIconVisibleInMenu(true);
	actNouveauFavoris->setData(QVariant::Int);
	connect(actNouveauFavoris, SIGNAL(triggered()), this, SLOT(actNouveauFavoris_triggered()));
	
	actGestFavoris=new QAction(QIcon(":coMexpert/images/bookmark_toolbar.png"), tr("&Gestion des favoris"), this);
	actGestFavoris->setIconVisibleInMenu(true);
	connect(actGestFavoris, SIGNAL(triggered()), this, SLOT(actGestFavoris_triggered()));
	
	actSeparator=favorisMenu->addSeparator();
	favorisMenu->addAction(actNouveauFavoris);
	favorisMenu->addAction(actGestFavoris);
	loadFavoris(favorisMenu);

	actFavorisClient->setMenu(favorisMenu);
}
//------------------------------------------------------------------------------
void CClientFrm::loadFavoris(QMenu *menu, int *parent) {
	QString requete;
	QSqlQuery *query;
	
	requete="SELECT fc.id, fc.client, fc.nom AS folderName, c.nom AS clientName ";
	requete+="FROM favorisClient AS fc ";
	requete+="LEFT JOIN client AS c ON c.code=fc.client ";
	requete+="WHERE user=\""+CMySQL::getInstance()->getUser()+"\" AND ";
	requete+="parent "+(parent == 0 ? "IS NULL" : "= "+QString::number(*parent))+" ";
	requete+="ORDER BY client";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		while(query->next()) {
			QSqlRecord rec=query->record();
			
			if(rec.value("client").isNull()) {
				int p=rec.value("id").toInt();
				QMenu *nMenu=menu;
				
				if(!rec.value("folderName").toString().isNull()) {
					nMenu=new QMenu(rec.value("folderName").toString());
					
					nMenu->setIcon(QIcon(":coMexpert/images/folder.png"));
					
					menu->insertMenu(actSeparator, nMenu);
					
					QAction *act=new QAction(QIcon(":coMexpert/images/bookmark_add.png"), tr("&Ajouter ce client comme favoris"), this);
					act->setIconVisibleInMenu(true);
					act->setData(rec.value("id"));
					connect(act, SIGNAL(triggered()), this, SLOT(actNouveauFavoris_triggered()));
					nMenu->addAction(act);
					nMenu->addSeparator();
				}
				
				loadFavoris(nMenu, &p);
			}else {
				QAction * act=new QAction(rec.value("clientName").toString(), this);
				act->setData(rec.value("client").toString());
				connect(act, SIGNAL(triggered()), this, SLOT(actSelectFavoris_triggered()));
				
				menu->insertAction(actSeparator, act);
			}
		}
	}
	delete query;
}
//------------------------------------------------------------------------------
void CClientFrm::loadActionInfo(QString action, int curRow) {
	QSqlQuery *query;
	QString requete;
		
	requete="SELECT nom, ";
	requete+="MAKEDATE(YEAR(CURDATE()), DAYOFYEAR(DATE_FORMAT(CURDATE(), \"%Y-%m-01\"))+jourEchPref-1) AS nextEcheance, ";
	requete+="repete FROM action WHERE code = \""+action+"\"";

	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		if(query->next()) {
			QSqlRecord rec=query->record();
			
			echeancesWidget->item(curRow, cnecDescription)->setText(rec.value("code").toString());
			echeancesWidget->item(curRow, cnecDate)->setText(rec.value("nextEcheance").toDate().toString("dd/MM/yyyy"));
			echeancesWidget->item(curRow, cnecRepete)->setText(rec.value("repete").toString());
		}
	}
	delete query;
}
//------------------------------------------------------------------------------
void CClientFrm::dropFavoris(QMenu *menu) {
	QList<QAction*> listActions=menu->actions();
	QAction *act;
	int i=0;
	
	for(i=0;i<listActions.size();i++) {
		act=listActions[i];
		
		if(act == actSeparator) {
			break;
		}
		if(act->menu() != 0) {
			dropFavoris(act->menu());
		}
		favorisMenu->removeAction(act);
		delete act;
	}
}
//------------------------------------------------------------------------------
void CClientFrm::makeRequete(QString &requete, QString &alias) {
	requete="SELECT code, nom, adresse1, adresse2, adresse3, codePostal, ville, pays, telephone, ";
	requete+="fax, creation, modeReglement, conditionReglement, prospect, activite, effectif, ";
	requete+="chef, indirect, documentPar, utilisateur, idSociete, compteComptable ";
	requete+="FROM "+masterTable;
}
//------------------------------------------------------------------------------
void CClientFrm::showFormulaire(QSqlQuery *query, CGlobal::EAction& action) {
	QSqlRecord rec=query->record();
	
	curCode=rec.value("code").toString();
	code->setValue(curCode);
	nom->setValue(rec.value("nom"));
	adresse1->setValue(rec.value("adresse1"));
	adresse2->setValue(rec.value("adresse2"));
	adresse3->setValue(rec.value("adresse3"));
	codePostal->setValue(rec.value("codePostal"));
	ville->setValue(rec.value("ville"));
	pays->setValue(rec.value("pays"));
	telephone->setValue(rec.value("telephone"));
	fax->setValue(rec.value("fax"));
	creation->setValue(rec.value("creation"));
	modeReglement->setValue(rec.value("modeReglement"));
	conditionReglement->setValue(rec.value("conditionReglement"));
	prospect->setValue(rec.value("prospect"));
	activite->setValue(rec.value("activite"));
	effectif->setCurrentIndex(effectif->findText(rec.value("effectif").toString()));
	//cmtrsWidget->setValue(rec.value("commentaires").toString());
	chef->setValue(rec.value("chef"));
	indirect->setValue(rec.value("indirect"));
	
	alert->setPixmap(rec.value("indirect").toString() == "OUI" ? QPixmap(":coMexpert/images/alert.png") : QPixmap());
	alert->setToolTip(rec.value("indirect").toString() == "OUI" ? tr("Attention, client indirect !") : tr(""));
	documentPar->setCurrentIndex(documentPar->findText(rec.value("documentPar").toString()));
	utilisateur->setValue(rec.value("utilisateur"));
	idSociete->setValue(rec.value("idSociete"));
	compteComptable->setValue(rec.value("compteComptable"));
		
	loadGrilleActions();
	loadGrilleContact();
	loadGrilleAdresses();
	loadGrilleDevis();
	loadGrilleCommandes();
	loadGrilleBLs();
	loadGrilleFactures();
	loadGrillePrixArticle();
	loadGrilleDocuments();
	loadGrilleEcheances();
	loadGrilleMateriels();
	loadGrilleTva();
	loadCommentaires();
}
//------------------------------------------------------------------------------
void CClientFrm::loadCommentaires(void) {
	QString cmtrs;
	int nbRevision=makeCommentaires(cmtrs);
	
	cmtrsWidget->clearRevision();

	for(int i=0;i<nbRevision;i++) {
		cmtrsWidget->addRevision(QString::number(i+1));
	}
	
	cmtrsWidget->setToLastRevision();
	cmtrsWidget->setValue(cmtrs);
	tabWidget->setTabIcon(etnCommentaires, cmtrsWidget->getValue() != ""  ? QIcon(":coMexpert/images/irkickflash.png") : QIcon());
}
//------------------------------------------------------------------------------
int CClientFrm::makeCommentaires(QString &cmtrs, int revision) {
	QSqlQuery *query;
	QString requete;
	cmtrs="";
	int nbRevision=0;
	
	requete="SELECT diff, revision FROM commentairesClient WHERE codeClient = \""+curCode+"\" ";
	if(revision != 0) {
		requete+="AND revision <= "+QString::number(revision)+" ";
	}
	requete+="ORDER BY revision";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		while(query->next()) {
			QSqlRecord rec=query->record();
			
			cmtrs=CDiff::merge(cmtrs, rec.value("diff").toString());
			nbRevision++;
		}
	}
		
	delete query;
	
	return nbRevision;
}
//------------------------------------------------------------------------------
void CClientFrm::on_actAjouter_triggered(void) {
	contact->setText("");
	fonction->setText("");
	actionWidget->setRowCount(0);
	contactWidget->setRowCount(0);
	adressesWidget->setRowCount(0);
	devisWidget->setRowCount(0);
	commandesWidget->setRowCount(0);
	blsWidget->setRowCount(0);
	facturesWidget->setRowCount(0);
	prixArticleWidget->setRowCount(0);
	documentsWidget->setRowCount(0);
	echeancesWidget->setRowCount(0);
	materielsWidget->setRowCount(0);
	tvaWidget->setRowCount(0);
	tabWidget->setTabIcon(etnCommentaires, QIcon());
	alert->setPixmap(QPixmap());
	alert->setToolTip(tr(""));
	effectif->setCurrentIndex(0);
	documentPar->setCurrentIndex(0);
	mail->setText("");
	tabWidget->setTabText(etnContacts, tr("Contacts")+" (0)");
	
	setActionCur(CGlobal::acAjout);

	utilisateur->setValue(CMySQL::getInstance()->getUser());
	creation->setValue(CMySQL::getInstance()->getCurrentDate());
}
//------------------------------------------------------------------------------
void CClientFrm::on_actValider_triggered(void) {
	QString requete;
	QString condition;
	
	code->setText(code->text().toUpper());
	calculChamps(getActionCur());
	
	if(!validChamps()) return;
	if(!validLiaison()) return;
	
	switch(getActionCur()) {
		case CGlobal::acAjout:
		case CGlobal::acAjoutFromOther:
			requete="INSERT INTO "+masterTable+" SET "+primaryKey+"=\""+code->getValue()+"\", ";
			requete+="creation=CURDATE(), createur=\""+CMySQL::getInstance()->getUser()+"\", ";
			condition="";
			break;
		case CGlobal::acModif:
			requete="UPDATE "+masterTable+" SET ";
			requete+="modification=CURDATE(), modificateur=\""+CMySQL::getInstance()->getUser()+"\", ";
			condition="WHERE "+primaryKey+"=\""+code->getValue()+"\"";
			break;
		default:
			return;
	}
	
	requete+="nom=\""+nom->getValue()+"\", adresse1=\""+adresse1->getValue()+"\", "; 
	requete+="adresse2=\""+adresse2->getValue()+"\", adresse3=\""+adresse3->getValue()+"\", "; 
	requete+="codePostal=\""+codePostal->getValue()+"\", ville=\""+ville->getValue()+"\", ";
	requete+="telephone=\""+telephone->getValue()+"\", ";
	requete+="fax=\""+fax->getValue()+"\", pays=\""+pays->getValue()+"\", ";
	requete+="modeReglement=\""+modeReglement->getValue()+"\", ";
	requete+="conditionReglement=\""+conditionReglement->getValue()+"\", ";
	requete+="prospect=\""+prospect->getValue()+"\", ";
	requete+="activite=\""+activite->text()+"\", ";
	requete+="effectif=\""+effectif->currentText()+"\", ";//commentaires=\""+cmtrsWidget->getValue()+"\", ";
	requete+="chef=\""+chef->getValue()+"\", indirect=\""+indirect->getValue()+"\", ";
	requete+="documentPar=\""+documentPar->currentText()+"\", utilisateur = \""+utilisateur->getValue()+"\", ";
	requete+="idSociete=\""+idSociete->getValue()+"\", compteComptable=\""+compteComptable->getValue()+"\" ";
	
	requete+=condition;
	
	if(CMySQL::getInstance()->execQuery(requete)) {
		CGlobal::EAction action=getActionCur();
		curCode=code->text();
		tabWidget->setTabIcon(etnCommentaires, cmtrsWidget->getValue() != ""  ? QIcon(":coMexpert/images/irkickflash.png") : QIcon());
		
		alert->setPixmap(indirect->getValue() == "OUI" ? QPixmap(":coMexpert/images/alert.png") : QPixmap());
		alert->setToolTip(indirect->getValue() == "OUI" ? tr("Attention, client indirect !") : tr(""));
		
		if(action == CGlobal::acModif && responsableModif) {
			requete="UPDATE echeancesClient SET destinataire = \""+utilisateur->getValue()+"\" WHERE client = \""+curCode+"\"";
			
			CMySQL::getInstance()->execQuery(requete);
			
			loadGrilleEcheances();
		}
		
		if(cmtrsChange) {
			int nextRevision=CMySQL::getInstance()->getNextCode("commentairesClient", "revision", "codeClient=\""+curCode+"\"").toInt();
			QString cmtrs;
			QString diff;
			
			makeCommentaires(cmtrs);
			cmtrs.replace("\"", "\\\"").trimmed();
			diff=CDiff::diff(cmtrs, cmtrsWidget->getValue());
			
			if(diff != "") {
				requete="INSERT INTO commentairesClient (codeClient, revision, diff) ";
				requete+="SELECT \""+curCode+"\", "+QString::number(nextRevision)+", ";
				requete+="\""+diff+"\"";
				
				if(CMySQL::getInstance()->execQuery(requete)) {
					cmtrsWidget->addRevision(QString::number(nextRevision));
					cmtrsWidget->setToLastRevision();
				}
			}
		}
		
		setActionCur(CGlobal::acNone);
		
		if(action == CGlobal::acAjout) {
			loadGrilleTva();
		} else if(action == CGlobal::acAjoutFromOther) {
			requete="INSERT INTO actionsClient ";
			requete+="(client, numeroAction, action, dateAction, interlocuteur, commentaires, qui, heureAction, auto) ";
			requete+="SELECT \""+curCode+"\", numeroAction, action, dateAction, interlocuteur, commentaires, qui, heureAction, auto ";
			requete+="FROM actionsProspect WHERE prospect = \""+prospect->getValue()+"\" ";
			
			CMySQL::getInstance()->execQuery(requete);
			
			requete="INSERT INTO contactClient ";
			requete+="(client, numero, defaut, nom, fonction, telephone, fax, mail) ";
			requete+="SELECT \""+curCode+"\", numero, defaut, nom, fonction, telephone, fax, mail ";
			requete+="FROM contactProspect WHERE prospect = \""+prospect->getValue()+"\" ";
			
			CMySQL::getInstance()->execQuery(requete);

			requete="INSERT INTO documentsClient ";
			requete+="(client, numero, description, typeMime, document, fileName, createur, creation, modificateur, modification) ";
			requete+="SELECT \""+curCode+"\", numero, description, typeMime, document, fileName, createur, creation, modificateur, modification ";
			requete+="FROM documentsProspect WHERE prospect = \""+prospect->getValue()+"\" ";
			
			CMySQL::getInstance()->execQuery(requete);

			requete="INSERT INTO favorisClient ";
			requete+="(user, client, parent) ";
			requete+="SELECT \""+CMySQL::getInstance()->getUser()+"\", \""+curCode+"\", fp.id ";
			requete+="FROM favorisProspect AS fp WHERE fp.prospect = \""+prospect->getValue()+"\" AND fp.user=\""+CMySQL::getInstance()->getUser()+"\"";
			
			CMySQL::getInstance()->execQuery(requete);
			dropFavoris(favorisMenu);
			loadFavoris(favorisMenu);
			
			requete="update teteDevis SET codeClient=\""+curCode+"\", codeProspect=\"\" ";
			requete+="WHERE codeProspect = \""+prospect->getValue()+"\" ";
			
			CMySQL::getInstance()->execQuery(requete);
			
			QString requete="DELETE FROM prospect WHERE code=\""+prospect->getValue()+"\"";

			CMySQL::getInstance()->execQuery(requete);
			
			loadGrilleActions();
			loadGrilleContact();
			loadGrilleDevis();
			loadGrilleDocuments();
			loadGrilleTva();
			loadGrilleEcheances();
		}
	}
}
//------------------------------------------------------------------------------
void CClientFrm::on_actAnnuler_triggered(void) {
	
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir annuler ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes) {
		if(!chargeDonnees(curCode, CGlobal::seNone)) {
			if(!chargeDonnees(curCode, CGlobal::sePrecedent)) {
				if(!chargeDonnees(curCode, CGlobal::seSuivant)) {
					setActionCur(CGlobal::acTableVide);
					return;
				}
			}
		}
	}
}
//------------------------------------------------------------------------------
void CClientFrm::on_actProspect_triggered(void) {
	CComboResultFrm *comboResultFrm=new CComboResultFrm("prospect", "code", this);
		
	comboResultFrm->setModal(true);
	comboResultFrm->exec();

	if(comboResultFrm->result() == QDialog::Accepted) {
		actionWidget->setRowCount(0);
		contactWidget->setRowCount(0);
		devisWidget->setRowCount(0);
		commandesWidget->setRowCount(0);
		blsWidget->setRowCount(0);
		facturesWidget->setRowCount(0);
		prixArticleWidget->setRowCount(0);
		documentsWidget->setRowCount(0);
		echeancesWidget->setRowCount(0);
		
		setActionCur(CGlobal::acAjoutFromOther);
		creation->setValue(CMySQL::getInstance()->getCurrentDate());
		
		loadProspectInfo(comboResultFrm->getResult());
	}
		
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CClientFrm::on_actSupprimer_triggered(void) {
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir supprimer la fiche courante ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes) {
		if(!CMySQL::getInstance()->canDelete("teteDevis", "codeClient", curCode))
			return;
		if(!CMySQL::getInstance()->canDelete("teteCommandes", "codeClient", curCode))
			return;
		if(!CMySQL::getInstance()->canDelete("teteBLs", "codeClient", curCode))
			return;
		if(!CMySQL::getInstance()->canDelete("teteFactures", "codeClient", curCode))
			return;
		
		QString requete="DELETE FROM "+masterTable+" WHERE "+primaryKey+"=\""+curCode+"\"";
		CMySQL::getInstance()->execQuery(requete);
		
		dropFavoris(favorisMenu);
		loadFavoris(favorisMenu);
		
		if(!chargeDonnees(curCode, CGlobal::sePrecedent)) {
			if(!chargeDonnees(curCode, CGlobal::seSuivant)) {
				setActionCur(CGlobal::acTableVide);
			}
		}
	}
}
//------------------------------------------------------------------------------
void CClientFrm::onChange(void) {
	if(getActionCur() == CGlobal::acNone) {
		setActionCur(CGlobal::acModif);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::onCmtrsChange(void) {
	onChange();
	cmtrsChange=true;
}
//------------------------------------------------------------------------------
void CClientFrm::on_actRechercheUneFiche_triggered(void) {
	CFindFrm *findFrm=new CFindFrm(masterTable, this);
	
	findFrm->setModal(true);
	findFrm->exec();
	if(findFrm->result() == QDialog::Accepted) {
		chargeDonnees(findFrm->getResult(), CGlobal::seNone);
	}
	
	delete findFrm;
}
//------------------------------------------------------------------------------
void CClientFrm::on_actEvolution_triggered(void) {
	QString url=CGlobal::getConfig().value("HTTPHost").toString()+"/"+CGlobal::md5Hash(CMySQL::getInstance()->getBase())+"/evolutionClient.php?codeClient="+curCode;
	
	QDesktopServices::openUrl(QUrl(url));
}
//------------------------------------------------------------------------------
void CClientFrm::on_actAbonneEMailing_triggered(void) {
	QString eMail=mail->text().section(">", 1, 1).section("<", 0, 0);
	
	if(eMail != "") {
		if(!CMySQL::getInstance()->existe("abonneEMailing", "mail", eMail)) {
			QString requete="INSERT INTO abonneEMailing SET id = \""+QUuid::createUuid().toString()+"\", ";
			requete+="mail = \""+eMail+"\", abonne=1, creation=CURDATE(), createur=\""+CMySQL::getInstance()->getUser()+"\", ";
			requete+="societe = \""+nom->getValue()+"\", contact = \""+contact->text()+"\" ";
					
			CMySQL::getInstance()->execQuery(requete);
			
			QMessageBox msgBox(	QMessageBox::Information, tr("Information"), 
								tr("Le client a été ajouter aux abonnements EMailing"), QMessageBox::Ok);
			msgBox.exec();
		}else {
			QMessageBox msgBox(	QMessageBox::Warning, tr("Attention"), 
								tr("Le client a déjà été on_actAjouter_triggered aux abonnements EMailing"), QMessageBox::Ok);
			msgBox.exec();
		}
	}
}
//------------------------------------------------------------------------------
void CClientFrm::onCombo(CMyComboBox *comboBox) {
	CComboResultFrm *comboResultFrm=new CComboResultFrm(comboBox->getTableRemp(), comboBox->getChampRemp(), this);
	
	comboResultFrm->setModal(true);
	comboResultFrm->exec();
	
	if(comboResultFrm->result() == QDialog::Accepted) {
		comboBox->setValue(comboResultFrm->getResult());
	}
	
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CClientFrm::onMailTo(QString href) {
	if(href != "") {
		QDesktopServices::openUrl(href);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::ajouteAction(bool &goToLast, int numContact) {
	int col;
	QString contactNom;
	QString numeroAction=CMySQL::getInstance()->getNextCode("actionsClient", "numeroAction", "client=\""+curCode+"\"");
	QString requete;
	CGlobal::EAction oldActionCur=getActionCur();
	
	if(numContact == -1) {
		contactNom=contact->text();
	}else {
		QSqlQuery *query;
		
		requete="SELECT nom FROM contactClient ";
		requete+="WHERE client = \""+curCode+"\" AND numero = "+QString::number(numContact);
				
		if(CMySQL::getInstance()->execQuery(requete, &query)) {
			if(query->next()) {
				QSqlRecord rec=query->record();
				
				contactNom=rec.value("nom").toString();
			}
		}
		delete query;
	}
	
	
	actionWidget->getColumnModel(cnacInterloc)->setDefaultValue(contactNom);
	actionWidget->getColumnModel(cnacNum)->setDefaultValue(numeroAction);
	actionWidget->getColumnModel(cnacQui)->setDefaultValue(CMySQL::getInstance()->getUser());
	actionWidget->getColumnModel(cnacHeure)->setDefaultValue(CMySQL::getInstance()->getCurrentTime().toString("hh:mm:ss"));
	
	requete="INSERT INTO actionsClient ";
	requete+="SET client=\""+curCode+"\", numeroAction="+numeroAction+", ";
	requete+="dateAction=curdate(), interlocuteur=\""+contactNom+"\", ";
	requete+="qui=\""+CMySQL::getInstance()->getUser()+"\", ";
	requete+="heureAction=\""+actionWidget->getColumnModel(cnacHeure)->getDefaultValue()+"\" ";
	
	CMySQL::getInstance()->execQuery(requete);
	
	setActionCur(CGlobal::acAddLigne);
	actionWidget->insertRow(0);
	
	for(col=0;col<actionWidget->getColumnModelCount();col++) {
		CMyTableWidgetItem *item;
		CColumnModel *columnModel=actionWidget->getColumnModel(col);
				
		item=new CMyTableWidgetItem(columnModel->getDefaultValue(), columnModel->getType());
		item->setTextAlignment(columnModel->getAlignment() | Qt::AlignVCenter);
		item->setIcon(*columnModel->getIcon());
		item->setFlags(columnModel->getFlags());
		
		actionWidget->setItem(0, col, item);
	}
	
	setActionCur(oldActionCur);
}
//------------------------------------------------------------------------------
void CClientFrm::supprimeAction(int row, bool& ok) {
	QTableWidgetItem *item=actionWidget->item(row, 0);
	int numeroAction;
	
	if(item != NULL) {
		numeroAction=item->text().toInt();
		
		QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
							tr("Etes vous sûre de vouloir supprimer la ligne numero ")+
									QString().number(numeroAction)+tr(" ?"), QMessageBox::Yes | QMessageBox::No);
		msgBox.exec();
		
		if(msgBox.result() == QMessageBox::Yes) {
			QString requete;
			
			requete="DELETE from actionsClient WHERE client = \""+curCode+"\" AND numeroAction = "+QString().number(numeroAction);
			
			CMySQL::getInstance()->execQuery(requete);

			ok=true;
		}
		
	}
}
//------------------------------------------------------------------------------
void CClientFrm::modifiAction(int row, int column) {
	QString text;
	QString requete;
	QTableWidgetItem *item;
	
	if(getActionCur() != CGlobal::acNone && getActionCur() != CGlobal::acModif) return;
	item=actionWidget->item(row, column);
	
	if(item != NULL && column != cnacNum) {
		CColumnModel *columnModel=actionWidget->getColumnModel(column);
		QString text=columnModel->format(item->text());
		QString textSQL=columnModel->formatToSQL(CGlobal::addSlashesQuot(item->text()));

		requete="UPDATE actionsClient ";
		requete+="SET "+columnModel->getNomChamp()+" = "+textSQL+" ";
		requete+="WHERE client=\""+curCode+"\" AND numeroAction="+actionWidget->item(row, cnacNum)->text();
		
		item->setText(text);
		
		CMySQL::getInstance()->execQuery(requete);
		
		actionWidget->resizeRowToContents(row);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::actionDoubleClicked(int row, int column) {
	QTableWidgetItem *item;
	CCalendrierFrm *calendrierFrm;
	CComboResultFrm *comboResultFrm;
		
	item=actionWidget->item(row, column);
	if(item->flags() != 0) {
		switch(column) {
			case cnacDate:
				calendrierFrm=new CCalendrierFrm(this);
				
				calendrierFrm->setDate(QDate::fromString(item->text(), "dd/MM/yyyy"));
				calendrierFrm->exec();
				
				if(calendrierFrm->result() == QDialog::Accepted) {
					item->setText(calendrierFrm->getDate().toString("dd/MM/yyyy"));
				}
				
				delete calendrierFrm;
				break;
			case cnacInterloc:
				comboResultFrm=new CComboResultFrm("contactClient", "nom", this, 1, "client = \""+curCode+"\" ");
		
				comboResultFrm->setModal(true);
				comboResultFrm->exec();
		
				if(comboResultFrm->result() == QDialog::Accepted) {
					item->setText(comboResultFrm->getResult());
				}
		
				delete comboResultFrm;
				break;
			default:
				break;
		}
	}
}
//------------------------------------------------------------------------------
void CClientFrm::ajouteContact(bool &goToLast) {
	int col;
	int row;
	QString numeroContact=CMySQL::getInstance()->getNextCode("contactClient", "numero", "client=\""+curCode+"\"");
	QString requete="INSERT INTO contactClient ";
	requete+="SET client=\""+curCode+"\", numero="+numeroContact+" ";
	
	contactWidget->getColumnModel(cnccNum)->setDefaultValue(numeroContact);
	
	CMySQL::getInstance()->execQuery(requete);
	
	setActionCur(CGlobal::acAddLigne);
	row=contactWidget->addRow();
	
	for(col=0;col<contactWidget->getColumnModelCount();col++) {
		CMyTableWidgetItem *item;
		CColumnModel * columnModel=contactWidget->getColumnModel(col);
				
		item=new CMyTableWidgetItem(columnModel->getDefaultValue(), columnModel->getType());
		item->setTextAlignment(columnModel->getAlignment() | Qt::AlignVCenter);
		item->setIcon(*columnModel->getIcon());
		item->setFlags(columnModel->getFlags());
		
		contactWidget->setItem(row, col, item);
		tabWidget->setTabText(etnContacts, tr("Contacts")+" ("+QString::number(row+1)+")");
	}
	
	setActionCur(CGlobal::acNone);
}
//------------------------------------------------------------------------------
void CClientFrm::supprimeContact(int row, bool& ok) {
	QTableWidgetItem *item=contactWidget->item(row, cnccNum);
	int numeroContact;
	
	if(item != NULL) {
		numeroContact=item->text().toInt();
		
		QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
							tr("Etes vous sûre de vouloir supprimer la ligne numero ")+
									QString().number(numeroContact)+tr(" ?"), QMessageBox::Yes | QMessageBox::No);
		msgBox.exec();
		
		if(msgBox.result() == QMessageBox::Yes) {
			QString requete;
			
			requete="DELETE from contactClient WHERE client = \""+curCode+"\" AND numero = "+QString().number(numeroContact);
			
			CMySQL::getInstance()->execQuery(requete);
			
			if(contactWidget->item(row, cnccDefaut)->text() == "X") {
				contact->setText("");
				fonction->setText("");
				mail->setText("");
			}
			
			tabWidget->setTabText(etnContacts, tr("Contacts")+" ("+QString::number(contactWidget->rowCount()-1)+")");
			
			ok=true;
		}
		
	}
}
//------------------------------------------------------------------------------
void CClientFrm::modifiContact(int row, int column) {
	QString requete;
	QTableWidgetItem *item;
	
	if(getActionCur() != CGlobal::acNone) return;
	item=contactWidget->item(row, column);
	
	if(item != NULL && column != cnccNum) {
		QString text=contactWidget->getColumnModel(column)->format(item->text());
		QString textSQL=contactWidget->getColumnModel(column)->formatToSQL(CGlobal::addSlashesQuot(item->text()));
		
		requete="UPDATE contactClient ";
		requete+="SET "+contactWidget->getColumnModel(column)->getNomChamp()+" = "+textSQL+" ";
		requete+="WHERE client=\""+curCode+"\" AND numero="+contactWidget->item(row, cnccNum)->text();
		
		item->setText(text);
		
		CMySQL::getInstance()->execQuery(requete);
		
		beforeChangeRecordContact(NULL, row);
		
		contactWidget->resizeRowToContents(row);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::contactDoubleClicked(int row, int column) {
	QString requete;
	QTableWidgetItem *item;
	int i;
	
	if(getActionCur() != CGlobal::acNone && getActionCur() != CGlobal::acModif) return;
	
	if(column == cnccDefaut) {
		item=contactWidget->item(row, column);
		
		if(item->text() != "X") {
			requete="UPDATE contactClient SET defaut=\"\" WHERE client=\""+curCode+"\" ";
			CMySQL::getInstance()->execQuery(requete);
			
			for(i=0;i<contactWidget->rowCount();i++) {
				contactWidget->item(i, column)->setText("");
			}
			
			requete="UPDATE contactClient ";
			requete+="SET defaut=\"X\" ";
			requete+="WHERE client=\""+curCode+"\" AND numero="+contactWidget->item(row, cnccNum)->text();
			CMySQL::getInstance()->execQuery(requete);
			
			item->setText("X");
			
			contact->setText(contactWidget->item(row, cnccNom)->text());
			fonction->setText(contactWidget->item(row, cnccFonction)->text());
			mail->setText(QString("<a href=\"mailto:%1\">%1</a>").arg(contactWidget->item(row, cnccMail)->text()));
		}
	}
}
//------------------------------------------------------------------------------
void CClientFrm::ajouteAdresse(bool &goToLast) {
	int col;
	int row;
	QString numeroAdresse=CMySQL::getInstance()->getNextCode("adressesClient", "numero", "client=\""+curCode+"\"");
	QString requete="INSERT INTO adressesClient ";
	requete+="SET client=\""+curCode+"\", numero="+numeroAdresse+" ";
	
	adressesWidget->getColumnModel(cnadcNum)->setDefaultValue(numeroAdresse);
	
	CMySQL::getInstance()->execQuery(requete);
	
	setActionCur(CGlobal::acAddLigne);
	row=adressesWidget->addRow();
	
	for(col=0;col<adressesWidget->getColumnModelCount();col++) {
		CMyTableWidgetItem *item;
		CColumnModel * columnModel=adressesWidget->getColumnModel(col);
				
		item=new CMyTableWidgetItem(columnModel->getDefaultValue(), columnModel->getType());
		item->setTextAlignment(columnModel->getAlignment() | Qt::AlignVCenter);
		item->setIcon(*columnModel->getIcon());
		item->setFlags(columnModel->getFlags());
		
		adressesWidget->setItem(row, col, item);
	}
	
	setActionCur(CGlobal::acNone);
}
//------------------------------------------------------------------------------
void CClientFrm::supprimeAdresse(int row, bool& ok) {
	QTableWidgetItem *item=adressesWidget->item(row, cnadcNum);
	int numeroAdresse;
	
	if(item != NULL) {
		numeroAdresse=item->text().toInt();
		
		QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
							tr("Etes vous sûre de vouloir supprimer la ligne numero ")+
									QString().number(numeroAdresse)+tr(" ?"), QMessageBox::Yes | QMessageBox::No);
		msgBox.exec();
		
		if(msgBox.result() == QMessageBox::Yes) {
			QString requete;
			
			requete="DELETE from adressesClient WHERE client = \""+curCode+"\" AND numero = "+QString().number(numeroAdresse);
			
			CMySQL::getInstance()->execQuery(requete);
			
			ok=true;
		}
		
	}
}
//------------------------------------------------------------------------------
void CClientFrm::modifiAdresse(int row, int column) {
	QString requete;
	QTableWidgetItem *item;
	
	if(getActionCur() != CGlobal::acNone) return;
	item=adressesWidget->item(row, column);
	
	if(item != NULL && column != cnadcNum) {
		QString text=adressesWidget->getColumnModel(column)->format(item->text());
		QString textSQL=adressesWidget->getColumnModel(column)->formatToSQL(CGlobal::addSlashesQuot(item->text()));
		
		requete="UPDATE adressesClient ";
		requete+="SET "+adressesWidget->getColumnModel(column)->getNomChamp()+" = "+textSQL+" ";
		requete+="WHERE client=\""+curCode+"\" AND numero="+adressesWidget->item(row, cnadcNum)->text();
		
		item->setText(text);
		
		CMySQL::getInstance()->execQuery(requete);
		
		adressesWidget->resizeRowToContents(row);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::adresseDoubleClicked(int row, int column) {
	CComboResultFrm *comboResultFrm;
	
	if(getActionCur() != CGlobal::acNone && getActionCur() != CGlobal::acModif) return;
	
	if(column == cnadcPays) {
		comboResultFrm=new CComboResultFrm("pays", "nom", this);
		
		comboResultFrm->setModal(true);
		comboResultFrm->exec();
		
		if(comboResultFrm->result() == QDialog::Accepted) {
			QTableWidgetItem *item;
		
			item=adressesWidget->item(row, column);
				
			item->setText(comboResultFrm->getResult());
		}
		
		delete comboResultFrm;
	}
}
//------------------------------------------------------------------------------
void CClientFrm::devisDoubleClicked(int row, int column) {
	if(column == cndcNum) {
		int numero=devisWidget->item(row, column)->text().toInt();
		
		mainFrm->loadDevis(numero);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::commandesDoubleClicked(int row, int column) {
	if(column == cncocNom) {
		int numero=commandesWidget->item(row, column)->data(Qt::UserRole).toInt();
		
		mainFrm->loadCommandes(numero);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::blsDoubleClicked(int row, int column) {
	if(column == cnbcNom) {
		int numero=blsWidget->item(row, column)->data(Qt::UserRole).toInt();
		
		mainFrm->loadBLs(numero);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::facturesDoubleClicked(int row, int column) {
	if(column == cnfcNum) {
		int numero=facturesWidget->item(row, column)->text().toInt();
		
		mainFrm->loadFactures(numero);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::ajoutePrixArticle(bool &goToLast) {
	int col;
	int row;
	QString numero=CMySQL::getInstance()->getNextCode("prixArticleClient", "numero", "client=\""+curCode+"\"");
	QString requete="INSERT INTO prixArticleClient ";
	requete+="SET client=\""+curCode+"\", numero="+numero+", createur=\""+CMySQL::getInstance()->getUser()+"\", creation=CURDATE() ";
	
	prixArticleWidget->getColumnModel(cnccNum)->setDefaultValue(numero);
	
	CMySQL::getInstance()->execQuery(requete);
	
	setActionCur(CGlobal::acAddLigne);
	row=prixArticleWidget->addRow();
	
	for(col=0;col<prixArticleWidget->getColumnModelCount();col++) {
		CMyTableWidgetItem *item;
		CColumnModel *columnModel=prixArticleWidget->getColumnModel(col);
				
		item=new CMyTableWidgetItem(columnModel->getDefaultValue(), columnModel->getType());
		item->setTextAlignment(columnModel->getAlignment() | Qt::AlignVCenter);
		item->setIcon(*columnModel->getIcon());
		item->setFlags(columnModel->getFlags());
		
		prixArticleWidget->setItem(row, col, item);
	}
	
	setActionCur(CGlobal::acNone);
}
//------------------------------------------------------------------------------
void CClientFrm::supprimePrixArticle(int row, bool& ok) {
	QTableWidgetItem *item=prixArticleWidget->item(row, cnccNum);
	int numero;
	
	if(item != NULL) {
		numero=item->text().toInt();
		
		QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
							tr("Etes vous sûre de vouloir supprimer le prix numero ")+
									QString::number(numero)+tr(" ?"), QMessageBox::Yes | QMessageBox::No);
		msgBox.exec();
		
		if(msgBox.result() == QMessageBox::Yes) {
			QString requete;
			
			requete="DELETE from prixArticleClient WHERE client = \""+curCode+"\" AND numero = "+QString::number(numero);
			
			CMySQL::getInstance()->execQuery(requete);
			
			ok=true;
		}
	}
}
//------------------------------------------------------------------------------
void CClientFrm::modifiPrixArticle(int row, int column) {
	QString requete;
	QTableWidgetItem *item;
	
	if(getActionCur() != CGlobal::acNone) return;
	item=prixArticleWidget->item(row, column);
	
	if(item != NULL) {
		QString text=prixArticleWidget->getColumnModel(column)->format(item->text());
		QString textSQL=prixArticleWidget->getColumnModel(column)->formatToSQL(CGlobal::addSlashesQuot(item->text()));
		
		requete="UPDATE prixArticleClient ";
		requete+="SET "+prixArticleWidget->getColumnModel(column)->getNomChamp()+" = "+textSQL+", ";
		requete+="modificateur=\""+CMySQL::getInstance()->getUser()+"\", modification=CURDATE() ";
		requete+="WHERE client=\""+curCode+"\" AND numero="+prixArticleWidget->item(row, cnpaNum)->text();
		
		item->setText(text);
		
		CMySQL::getInstance()->execQuery(requete);
		
		prixArticleWidget->resizeRowToContents(row);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::lignePrixArticleDoubleClicked(int row, int column) {
	CComboResultFrm *comboResultFrm;
	
	switch(column) {
		case cnpaArticle:
			comboResultFrm=new CComboResultFrm("article", "code", this);
		
			comboResultFrm->setModal(true);
			comboResultFrm->exec();
		
			if(comboResultFrm->result() == QDialog::Accepted) {
				QTableWidgetItem *item;
		
				item=prixArticleWidget->item(row, column);
				
				item->setText(comboResultFrm->getResult());
			}
		
			delete comboResultFrm;
			break;
		default:
			break;
	}
}
//------------------------------------------------------------------------------
void CClientFrm::ajouteDocument(bool &goToLast) {
	int col;
	int row;
	QString numero=CMySQL::getInstance()->getNextCode("documentsClient", "numero", "client=\""+curCode+"\"");
	QString requete="INSERT INTO documentsClient ";
	requete+="SET client=\""+curCode+"\", numero="+numero+", createur=\""+CMySQL::getInstance()->getUser()+"\", creation=CURDATE() ";
	
	documentsWidget->getColumnModel(cnpdNum)->setDefaultValue(numero);
	
	CMySQL::getInstance()->execQuery(requete);
	
	setActionCur(CGlobal::acAddLigne);
	row=documentsWidget->addRow();
	
	for(col=0;col<documentsWidget->getColumnModelCount();col++) {
		CMyTableWidgetItem *item;
		CColumnModel *columnModel=documentsWidget->getColumnModel(col);
				
		item=new CMyTableWidgetItem(columnModel->getDefaultValue(), columnModel->getType());
		item->setTextAlignment(columnModel->getAlignment() | Qt::AlignVCenter);
		item->setIcon(*columnModel->getIcon());
		item->setFlags(columnModel->getFlags());
		
		documentsWidget->setItem(row, col, item);
	}
	
	creerDocument(row);
	setActionCur(CGlobal::acNone);
}
//------------------------------------------------------------------------------
void CClientFrm::supprimeDocument(int row, bool& ok) {
	QTableWidgetItem *item=documentsWidget->item(row, cnccNum);
	int numero;
	
	if(item != NULL) {
		numero=item->text().toInt();
		
		QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
							tr("Etes vous sûre de vouloir supprimer le document numero ")+
									QString::number(numero)+tr(" ?"), QMessageBox::Yes | QMessageBox::No);
		msgBox.exec();
		
		if(msgBox.result() == QMessageBox::Yes) {
			QString requete;
			
			requete="DELETE from documentsClient WHERE client = \""+curCode+"\" AND numero = "+QString::number(numero);
			
			CMySQL::getInstance()->execQuery(requete);
			
			ok=true;
		}
	}
}
//------------------------------------------------------------------------------
void CClientFrm::modifiDocument(int row, int column) {
	QString requete;
	QTableWidgetItem *item;
	
	if(getActionCur() != CGlobal::acNone) return;
	item=documentsWidget->item(row, column);
	
	if(item != NULL) {
		QString text=documentsWidget->getColumnModel(column)->format(item->text());
		QString textSQL=documentsWidget->getColumnModel(column)->formatToSQL(CGlobal::addSlashesQuot(item->text()));
		
		requete="UPDATE documentsClient ";
		requete+="SET "+documentsWidget->getColumnModel(column)->getNomChamp()+" = "+textSQL+", ";
		requete+="modificateur=\""+CMySQL::getInstance()->getUser()+"\", modification=CURDATE() ";
		requete+="WHERE client=\""+curCode+"\" AND numero="+documentsWidget->item(row, cnpdNum)->text();
		
		item->setText(text);
		
		CMySQL::getInstance()->execQuery(requete);
		
		documentsWidget->resizeRowToContents(row);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::documentDoubleClicked(int row, int column) {
	QString url;
	
	switch(column) {
		case cnpdDocument:
			url=CGlobal::getConfig().value("HTTPHost").toString()+"/getDocument.php?client="+curCode+"&numero="+documentsWidget->item(row, cnpdNum)->text();
	
			QDesktopServices::openUrl(QUrl(url));
			break;
		case cnpdNum:
			creerDocument(row);
			break;
		default:
			break;
	}
}
//------------------------------------------------------------------------------
void CClientFrm::ajouteEcheance(bool &goToLast) {
	int col;
	int row;
	QString numero=CMySQL::getInstance()->getNextCode("echeancesClient", "numero", "client=\""+curCode+"\"");
	QString requete="INSERT INTO echeancesClient ";
	requete+="SET client=\""+curCode+"\", numero="+numero+", createur=\""+CMySQL::getInstance()->getUser()+"\", creation=CURDATE(), dateEcheance=CURDATE(), destinataire=\""+CMySQL::getInstance()->getUser()+"\" ";
	
	echeancesWidget->getColumnModel(cnecNum)->setDefaultValue(numero);
	echeancesWidget->getColumnModel(cnecDestinataire)->setDefaultValue(CMySQL::getInstance()->getUser());
	
	CMySQL::getInstance()->execQuery(requete);
	
	setActionCur(CGlobal::acAddLigne);
	row=echeancesWidget->addRow();
	
	for(col=0;col<echeancesWidget->getColumnModelCount();col++) {
		CMyTableWidgetItem *item;
		CColumnModel *columModel=echeancesWidget->getColumnModel(col);
				
		item=new CMyTableWidgetItem(columModel->getDefaultValue(), columModel->getType());
		item->setTextAlignment(columModel->getAlignment() | Qt::AlignVCenter);
		item->setIcon(*columModel->getIcon());
		item->setFlags(columModel->getFlags());
		
		echeancesWidget->setItem(row, col, item);
	}
	
	setActionCur(CGlobal::acNone);
}
//------------------------------------------------------------------------------
void CClientFrm::supprimeEcheance(int row, bool& ok) {
	QTableWidgetItem *item=echeancesWidget->item(row, cnecNum);
	int numero;
	
	if(item != NULL) {
		numero=item->text().toInt();
		
		QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
							tr("Etes vous sûre de vouloir supprimer l'échéance numero ")+
									QString::number(numero)+tr(" ?"), QMessageBox::Yes | QMessageBox::No);
		msgBox.exec();
		
		if(msgBox.result() == QMessageBox::Yes) {
			QString requete;
			
			requete="DELETE from echeancesClient WHERE client = \""+curCode+"\" AND numero = "+QString::number(numero);
			
			CMySQL::getInstance()->execQuery(requete);
			
			ok=true;
		}
	}
}
//------------------------------------------------------------------------------
void CClientFrm::modifiEcheance(int row, int column) {
	QString requete;
	QTableWidgetItem *item;
	
	if(getActionCur() != CGlobal::acNone) return;
	item=echeancesWidget->item(row, column);
	
	if(item != NULL) {
		QString text=echeancesWidget->getColumnModel(column)->format(item->text());
		QString textSQL=echeancesWidget->getColumnModel(column)->formatToSQL(CGlobal::addSlashesQuot(item->text()));
		
		requete="UPDATE echeancesClient ";
		requete+="SET "+echeancesWidget->getColumnModel(column)->getNomChamp()+" = "+textSQL+", ";
		requete+="modificateur=\""+CMySQL::getInstance()->getUser()+"\", modification=CURDATE() ";
		requete+="WHERE client=\""+curCode+"\" AND numero="+echeancesWidget->item(row, cnecNum)->text();
		
		item->setText(text);
		
		CMySQL::getInstance()->execQuery(requete);
		
		echeancesWidget->resizeRowToContents(row);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::echeanceDoubleClicked(int row, int column) {
	QTableWidgetItem *item;
	CCalendrierFrm *calendrierFrm;
	CComboResultFrm *comboResultFrm;
	
	item=echeancesWidget->item(row, column);
	switch(column) {
		case cnecAction:
			comboResultFrm=new CComboResultFrm("action", "code");
		
			comboResultFrm->setModal(true);
			comboResultFrm->exec();
		
			if(comboResultFrm->result() == QDialog::Accepted) {
				item->setText(comboResultFrm->getResult());

				loadActionInfo(comboResultFrm->getResult(), row);
			}
		
			delete comboResultFrm;
			break;
		case cnecDate:
			calendrierFrm=new CCalendrierFrm(this);
				
			calendrierFrm->setDate(QDate::fromString(item->text(), "dd/MM/yyyy"));
			calendrierFrm->exec();
				
			if(calendrierFrm->result() == QDialog::Accepted) {
				item->setText(calendrierFrm->getDate().toString("dd/MM/yyyy"));
			
				QString requete="UPDATE echeancesClient SET dateClique=\"0000-00-00\" ";
				requete+="WHERE client=\""+curCode+"\" AND numero="+echeancesWidget->item(row, cnecNum)->text();
				
				CMySQL::getInstance()->execQuery(requete);
			}
				
			delete calendrierFrm;
			break;
		case cnecDestinataire:
			comboResultFrm=new CComboResultFrm("utilisateur", "code");
		
			comboResultFrm->setModal(true);
			comboResultFrm->exec();
		
			if(comboResultFrm->result() == QDialog::Accepted) {
				item->setText(comboResultFrm->getResult());
			}
		
			delete comboResultFrm;
			break;
		default:
			break;
	}
}
//------------------------------------------------------------------------------
void CClientFrm::tableCustomContextMenuRequested(const QPoint & pos) {
	QTableWidgetItem *item=contactWidget->item(contactWidget->currentRow(), cnccNum);
	
	if(item != NULL) {
		curContactNum=item->text().toInt();
		actMailFromContact->setEnabled(contactWidget->item(contactWidget->currentRow(), cnccMail)->text() != "");
		popupMenu->popup(contactWidget->mapToGlobal(pos));
	}
}
//------------------------------------------------------------------------------
void CClientFrm::actActionFromContact_triggered(void) {
	if(curContactNum != -1) {
		bool goToLast=false;
		ajouteAction(goToLast, curContactNum);
		tabWidget->setCurrentIndex(0);
		
		curContactNum=-1;
	}
}
//------------------------------------------------------------------------------
void CClientFrm::actMailFromContact_triggered(void) {
	QString href=QString("mailto:%1").arg(contactWidget->item(contactWidget->currentRow(), cnccMail)->text());
	QDesktopServices::openUrl(href);
}
//------------------------------------------------------------------------------
void CClientFrm::ajouteMateriel(bool &goToLast) {
	int col;
	int row;
	QString numero=CMySQL::getInstance()->getNextCode("materielsClient", "numero", "client=\""+curCode+"\"");
	QString requete="INSERT INTO materielsClient ";
	requete+="SET client=\""+curCode+"\", numero="+numero+", dateFinGarantie=CURDATE() ";
	
	materielsWidget->getColumnModel(cnmcNum)->setDefaultValue(numero);
	
	CMySQL::getInstance()->execQuery(requete);
	
	setActionCur(CGlobal::acAddLigne);
	row=materielsWidget->addRow();
	
	for(col=0;col<materielsWidget->getColumnModelCount();col++) {
		CMyTableWidgetItem *item;
		CColumnModel *columnModel=materielsWidget->getColumnModel(col);
				
		item=new CMyTableWidgetItem(columnModel->getDefaultValue(), columnModel->getType());
		item->setTextAlignment(columnModel->getAlignment() | Qt::AlignVCenter);
		item->setIcon(*columnModel->getIcon());
		item->setFlags(columnModel->getFlags());
		
		materielsWidget->setItem(row, col, item);
	}
	
	setActionCur(CGlobal::acNone);
}
//------------------------------------------------------------------------------
void CClientFrm::supprimeMateriel(int row, bool& ok) {
	QTableWidgetItem *item=materielsWidget->item(row, cnmcNum);
	int numero;
	
	if(item != NULL) {
		numero=item->text().toInt();
		
		QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
							tr("Etes vous sûre de vouloir supprimer le matériel numero ")+
									QString::number(numero)+tr(" ?"), QMessageBox::Yes | QMessageBox::No);
		msgBox.exec();
		
		if(msgBox.result() == QMessageBox::Yes) {
			QString requete;
			
			requete="DELETE from materielsClient WHERE client = \""+curCode+"\" AND numero = "+QString::number(numero);
			
			CMySQL::getInstance()->execQuery(requete);
			
			ok=true;
		}
		
	}
}
//------------------------------------------------------------------------------
void CClientFrm::modifiMateriel(int row, int column) {
	QString requete;
	QTableWidgetItem *item;
	
	if(getActionCur() != CGlobal::acNone) return;
	item=materielsWidget->item(row, column);
	
	if(item != NULL) {
		QString text=materielsWidget->getColumnModel(column)->format(item->text());
		QString textSQL=materielsWidget->getColumnModel(column)->formatToSQL(CGlobal::addSlashesQuot(item->text()));
		
		requete="UPDATE materielsClient ";
		requete+="SET "+materielsWidget->getColumnModel(column)->getNomChamp()+" = "+textSQL+" ";
		requete+="WHERE client=\""+curCode+"\" AND numero="+materielsWidget->item(row, cnmcNum)->text();
		
		item->setText(text);
		
		CMySQL::getInstance()->execQuery(requete);
		
		materielsWidget->resizeRowToContents(row);
	}
}
//------------------------------------------------------------------------------
void CClientFrm::materielDoubleClicked(int row, int column) {
	QTableWidgetItem *item;
	CCalendrierFrm *calendrierFrm;
	CComboResultFrm *comboResultFrm;
	
	item=materielsWidget->item(row, column);
	switch(column) {
		case cnmcFacture:
			comboResultFrm=new CComboResultFrm("teteFactures", "code", this, 0, "codeClient=\""+curCode+"\"");
		
			comboResultFrm->setModal(true);
			comboResultFrm->exec();
		
			if(comboResultFrm->result() == QDialog::Accepted) {
				item->setText(comboResultFrm->getResult());
			}
		
			delete comboResultFrm;
			break;
		case cnmcDateFinGarantie:
			calendrierFrm=new CCalendrierFrm(this);
				
			calendrierFrm->setDate(QDate::fromString(item->text(), "dd/MM/yyyy"));
			calendrierFrm->exec();
				
			if(calendrierFrm->result() == QDialog::Accepted) {
				item->setText(calendrierFrm->getDate().toString("dd/MM/yyyy"));
			}
				
			delete calendrierFrm;
			break;
		default:
			break;
	}
}
//------------------------------------------------------------------------------
void CClientFrm::onInvokeHelp(CChamp *champ) {
	CHelp::getInstance()->startHelp("setSource qthelp://alpes-ressources.com.coMexpert-"+VERSION_LOGICIEL+"/doc/client.html#"+champ->getTable()+"."+champ->getChamp());	
}
//------------------------------------------------------------------------------
void CClientFrm::on_actFavorisClient_triggered(void) {
	favorisMenu->popup(QCursor::pos());
}
//------------------------------------------------------------------------------
void CClientFrm::actNouveauFavoris_triggered(void) {
	QAction *sender=(QAction *)QObject::sender();
	QString requete;
	QVariant parentId=sender->data();
	QSqlQuery *query;
	
	if(parentId.isNull()) {
		requete="SELECT id FROM favorisClient WHERE parent IS NULL AND user=\""+CMySQL::getInstance()->getUser()+"\"";
		
		if(CMySQL::getInstance()->execQuery(requete, &query)) {
			if(!query->next()) {
				requete="INSERT INTO favorisClient SET user=\""+CMySQL::getInstance()->getUser()+"\"";
				
				CMySQL::getInstance()->execQuery(requete, true, &parentId);
			}else {
				parentId=query->record().value("id");
			}
		}
		delete query;
	}
	
	requete="INSERT INTO favorisClient SET ";
	requete+="user=\""+CMySQL::getInstance()->getUser()+"\", client=\""+curCode+"\", parent="+parentId.toString();
	
	if(CMySQL::getInstance()->execQuery(requete)) {
		dropFavoris(favorisMenu);
		loadFavoris(favorisMenu);
	}	
}
//------------------------------------------------------------------------------
void CClientFrm::actGestFavoris_triggered(void) {
	CGestionFavorisClientFrm *gestionFavorisFrm=new CGestionFavorisClientFrm(this);
	
	gestionFavorisFrm->exec();
	
	dropFavoris(favorisMenu);
	loadFavoris(favorisMenu);
	
	delete gestionFavorisFrm;
}
//------------------------------------------------------------------------------
void CClientFrm::actSelectFavoris_triggered(void) {
	QAction *act = qobject_cast<QAction *>(this->sender());
	chargeDonnees(act->data().toString(), CGlobal::seNone);
}
//------------------------------------------------------------------------------
void CClientFrm::on_actEcheancier_triggered(void) {
	CEcheancierFrm *echeancierFrm=new CEcheancierFrm(this);
		
	echeancierFrm->setModal(true);
	echeancierFrm->exec();
	
	delete echeancierFrm;
}
//------------------------------------------------------------------------------
void CClientFrm::onResponsableChange(void) {
	if(getActionCur() == CGlobal::acNone) {
		setActionCur(CGlobal::acModif);
		responsableModif=true;
	}
}
//------------------------------------------------------------------------------
void CClientFrm::beforeInsertAction(const QSqlQuery *query, CMyTableWidgetItem *item) {
	QSqlRecord rec=query->record();
	
	if((rec.value("auto").toInt() == 1)) {
		item->setBackground(QBrush(QColor(170, 255, 170)));
	}
}
//------------------------------------------------------------------------------
void CClientFrm::beforeChangeRecordContact(const QSqlQuery *query, int row) {
	if(contactWidget->item(row, cnccDefaut)->text() == "X") {
		contact->setText(contactWidget->item(row, cnccNom)->text());
		fonction->setText(contactWidget->item(row, cnccFonction)->text());
		mail->setText(QString("<a href=\"mailto:%1\">%1</a>").arg(contactWidget->item(row, cnccMail)->text()));
	}
}
//------------------------------------------------------------------------------
void CClientFrm::onLoadCmtrsRevision(QString revision) {
	QString cmtrs;
	makeCommentaires(cmtrs, revision.toInt());
	
	cmtrsWidget->setValue(cmtrs);
	tabWidget->setTabIcon(etnCommentaires, cmtrsWidget->getValue() != ""  ? QIcon(":coMexpert/images/irkickflash.png") : QIcon());
}
//------------------------------------------------------------------------------
void CClientFrm::modifiTva(int row, int column) {
	QString text;
	QString requete;
	QTableWidgetItem *item;
	
	if(getActionCur() != CGlobal::acNone) return;
	item=tvaWidget->item(row, column);
	
	if(column != ctncId) {
		QString text=tvaWidget->getColumnModel(column)->format(item->text());
		QString textSQL=tvaWidget->getColumnModel(column)->formatToSQL(CGlobal::addSlashesQuot(item->text()));
		QString nomChamp=tvaWidget->getColumnModel(column)->getNomChamp();
		int id=tvaWidget->item(row, ctncId)->text().toInt();
		
		requete="UPDATE tvaClient ";
		requete+="SET "+nomChamp+"="+textSQL+", modification=CURDATE(), ";
		requete+="modificateur=\""+CMySQL::getInstance()->getUser()+"\", custom=\"OUI\" ";
		requete+="WHERE id="+QString::number(id);
		
		CMySQL::getInstance()->execQuery(requete);
	}
}
//------------------------------------------------------------------------------
